#!/usr/bin/env xonsh 

from fire import Fire

@Fire
def main(dirpath):
  cd @(dirpath)
  fileli = $(find @(dirpath)/*.json.gz).split()
  $action = "active" 
  echo "sessionTimestamp,firstSessionTimestamp,userId,android_id" > $action.csv
  for i in fileli:
    outfile=i[:-8]+".BackupRestoreActivity"
    print(outfile)
    pigz -dc @(i)|rg BackupRestoreActivity|jq -r '.sessionTimestamp[:-3]+","+.firstSessionTimestamp[:-3]+","+.userId+","+.deviceIdentifiers.AndroidId' > @(outfile) 
    cat @(outfile) >> $action
    rm -rf @(outfile)
    
  sort -n $action|uniq >> $action.csv
  rm -rf $action
  rm -rf $action.db
  (echo .mode csv;echo .import $action.csv $action)|sqlite3 $action.db
  zstd -19 --rm $action.db
  scp $action.db.zst r5:/root/zst/ 

